<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card
      ><el-alert
        title="注意：只允许为第三级分类设置相关参数"
        type="warning"
        :closable="false"
        show-icon
      >
      </el-alert>
      <!-- 选择商品分类区域 -->
      <el-row class="cat_opt">
        <el-col>
          <span>选择商品分类：</span>
          <!--  选择商品分类的级联选择器-->
          <el-cascader
            v-model="selectedKeys"
            :options="cateList"
            :props="cascaderProps"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-col>
      </el-row>
      <!-- tab页签区域 -->
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button :disabled="isBtnDisabled" type="primary" size="mini"
            >添加参数</el-button
          >
          <!-- 动态参数表格 -->
          <el-table :data="manyTabData" border stripe>
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column
              label="参数名称"
              prop="attr_name"
            ></el-table-column>
            <el-table-column label="操作">
              <template>
                <el-button type="primary" icon="el-icon-edit" size="mini"
                  >编辑</el-button
                >
                <el-button type="danger" icon="el-icon-delete" size="mini"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button :disabled="isBtnDisabled" type="primary" size="mini"
            >添加属性</el-button
          >
           <!-- 静态参数表格 -->
           <el-table :data="onlyTabData" border stripe>
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column
              label="属性名称"
              prop="attr_name"
            ></el-table-column>
            <el-table-column label="操作">
              <template>
                <el-button type="primary" icon="el-icon-edit" size="mini"
                  >编辑</el-button
                >
                <el-button type="danger" icon="el-icon-delete" size="mini"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [],
      cascaderProps: {
        // //可以选中一级内容
        // checkStrictly: true,
        //指定触发方式
        expandTrigger: 'hover',
        //数据源parantList中的cat_id做数据绑定
        value: 'cat_id',
        //数据源parantList的cat_name渲染出来的内容
        label: 'cat_name',
        //数据源parantList的children做嵌套
        children: 'children',
      },
      //   xua选中的父级 id数组
      selectedKeys: [],
      activeName: 'many',
      //   动态参数数据
      manyTabData: [],
      //   静态参数数据
      onlyTabData: [],
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status != 200)
        return this.$message.error('获取商品分类列表失败！')
      // console.log(res.data);
      this.cateList = res.data
    },
    handleChange() {
      // console.log(this.selectedKeys);
      this.getParamsData()
    },
    // tab页签点击事件
    handleTabClick() {
      //  console.log(this.activeName);
      this.getParamsData()
    },
    //
    async getParamsData() {
      if (this.selectedKeys.length != 3) {
        //    不是三级分类
        this.selectedKeys = []
        return
      }
      //   根据选择的三级分类ID和选择的Tab页签 获取对应的参数
      const { data: res } = await this.$http.get(
        `categories/${this.cateId}/attributes`,
        {
          params: {
            sel: this.activeName,
          },
        }
      )
      if (res.meta.status != 200)
        return this.$message.error('获取分类参数列表失败！')
      // console.log(res.data);
      if (this.activeName == 'many') {
        this.manyTabData = res.data
      } else {
        this.onlyTabData = res.data
      }
    },
  },
  computed: {
    //   如果按钮需要被禁用 返回true
    isBtnDisabled() {
      if (this.selectedKeys.length != 3) {
        return true
      }
      return false
    },
    //   当前选中的三级分类ID
    cateId() {
      if (this.selectedKeys.length == 3) {
        return this.selectedKeys[2]
      }
      return null
    },
  },
}
</script>

<style lang="less" scoped>
.cat_opt {
  margin: 15px 0;
}
</style>